package com.planet.db;

import com.planet.db.dao.*;
import org.apache.commons.lang.ArrayUtils;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternUtils;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;

/**
 * Created by danbai on 17/5/2.
 */
@Configuration
public class MyBatisConfig {

    @Bean
    public PlatformTransactionManager transactionManager(@Qualifier("dataSource") DataSource datasource) {
        return new DataSourceTransactionManager(datasource);

    }

    @Bean
    public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource")DataSource datasource, ResourceLoader resourceLoader) throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(datasource);
        ResourcePatternResolver rpr = ResourcePatternUtils.getResourcePatternResolver(resourceLoader);
        Resource[] configResource = rpr.getResources("classpath*:mybatis/sqlMapConfig.xml");
        if (ArrayUtils.isNotEmpty(configResource)) {
            factoryBean.setConfigLocation(configResource[0]);
        }
        Resource[] mapperResource = rpr.getResources("classpath*:mybatis/mapping/*.xml");
        factoryBean.setMapperLocations(mapperResource);

        return factoryBean.getObject();
    }

    private <T> T getMapper(SqlSessionFactory sqlSessionFactory, Class<T> mapperInterface) throws Exception {
        MapperFactoryBean<T> mapperFactoryBean = new MapperFactoryBean<T>();
        try {
            mapperFactoryBean.setSqlSessionFactory(sqlSessionFactory);
            mapperFactoryBean.setMapperInterface(mapperInterface);
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
        return mapperFactoryBean.getObject();
    }

    @Bean
    @Autowired
    public AssessmentUserMapper assessmentUserMapper(SqlSessionFactory sqlSessionFactory) throws Exception {
        return (AssessmentUserMapper)getMapper(sqlSessionFactory, AssessmentUserMapper.class);
    }

    @Bean
    @Autowired
    public CheckUserMapper checkUserMapper(SqlSessionFactory sqlSessionFactory) throws Exception {
        return (CheckUserMapper)getMapper(sqlSessionFactory, CheckUserMapper.class);
    }

    @Bean
    @Autowired
    public CompanyMapper companyMapper(SqlSessionFactory sqlSessionFactory) throws Exception {
        return (CompanyMapper)getMapper(sqlSessionFactory, CompanyMapper.class);
    }

    @Bean
    @Autowired
    public HouseholdMapper householdMapper(SqlSessionFactory sqlSessionFactory) throws Exception {
        return (HouseholdMapper)getMapper(sqlSessionFactory, HouseholdMapper.class);
    }

    @Bean
    @Autowired
    public UserMapper userMapper(SqlSessionFactory sqlSessionFactory) throws Exception {
        return (UserMapper)getMapper(sqlSessionFactory, UserMapper.class);
    }

}
